<?php
/**
 *用户模型类
 *@author hary <hary@taomee.com>
 */
class user_model extends model
{
	private $user;

	function __construct()
	{
		parent::__construct();

		$this->user = "user";
	}

	/**
	 * 添加一个用户
	 * @param $user
	 * @return boolean
	 */
	public function addUserItem($user){

		$response = $this->formatData($user);

		if(!is_array($response)) return false;

		$this->addDataBatch2($user,array(
		'u_name',
		'u_pass',
		'u_nick',
		'u_email',
		'u_phone',
		'u_addtime',
		'u_status',
		'u_des',
		'sort'));
			
		return $this->dataInsert($this->user);
	}


	/**
	 * 修改密码
	 * @return boolean
	 */
	public function changePwd($uid,$old_pwd,$new_pwd){

		if(!is_numeric($uid)) return false;

		$uid   = $this->formatData($uid);
		$old_pwd = md5($this->formatData($old_pwd));
		$new_pwd = md5($this->formatData($new_pwd)); 

		$user = $this->getUserItemById($uid);

		if($old_pwd == $user['u_pass'])
		{
			$this->addData("u_pass",$new_pwd);

			$this->where("u_id",$uid,'=','and',0);

			return $this->dataUpdate($this->user);
		}
		else
		{
			return false;
		}

	}

	/**
	 * 用户登录
	 * @param $uid
	 * @param $pwd
	 * @return max
	 */
	public function login($name,$pwd){

		$name = $this->formatData($name);
		$pwd  = md5($this->formatData($pwd));

		$user = $this->getUserItemByName($name);
		
		if($user['u_pass'] == $pwd)
		{
			return $user;
		}
		else
		{
			return false;
		}

	}

	/**
	 * 根据id删除用户
	 * @param $id
	 * @return max
	 */
	public function delUserItemById($id){

		if(!is_numeric($id)) return false;
			
		$this->where('u_id',$id,'=','and',0);
			
		return $this->dataDel($this->user);
	}

	/**
	 * 根据id获取获取user
	 * @param $id
	 * @return max
	 */
	public function getUserItemById($id){

		if(!is_numeric($id)) return false;
			
		$this->where('u_id',$id,'=','and',0);
			
		return $this->getDataInfo($this->user);
			
	}

	/**
	 * 根据name获取获取user
	 * @param $name
	 * @return max
	 */
	public function getUserItemByName($name){
			
		$name = $this->formatData($name);

		$this->where('u_name',$name,'=','and');
		return $this->getDataInfo($this->user);
			
	}

	/**
	 * 更改用户信息
	 * @param $user
	 * @return max
	 */
	public function changeUserItemById($user){
			
		$user = $this->formatData($user);
		$id   = intval($user['u_id']);
			
		if(!is_array($user)) return false;
			
		$this->addDataBatch2($user,array(
		'u_name',
		'u_pass',
		'u_nick',
		'u_email',
		'u_phone',
		'u_addtime',
		'u_status',
		'u_des',
		'sort'));
			
		$this->where('u_id',$id);
		
		return $this->dataUpdate($this->user);

	}

	/**
	 * 根据条件获取多个user信息
	 * @param $keywords
	 * @param $page
	 * @param $page_size
	 * @param $order
	 * @return max
	 */
	public function getUserItems($keywords="",$page='1',$page_size='10',$order='sort desc'){
			
		$keywords 	 = $this->formatData($keywords);
			
		$sql = "select * from $this->user where ";

		if($keywords)    $sql .= " and ( u_name like '%$keywords%' or u_nick like '%$keywords%' or u_des like '%$keywords%' ) ";
			
		if($page) $this->setPage($page);
			
		if($page_size) $this->setLimit($page_size);
			
		if($order) $this->setOrder(' order by '.$order);
			
		return $this->customQuery($sql);
	}

}